package com.action.baseinfo;
import java.text.ParseException;
import java.util.*;
import javax.annotation.Resource;
import org.springframework.data.domain.Sort;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import com.beans.ClassInfo;
import com.dao.ClassDao;

/**
 * 位于com.action.baseinfo 包下的所有信息都是基础信息,比如班级信息,教室信息等等
 */

//班级信息控制层
@Controller()
@RequestMapping("/base")
public class ClassAction {

	@Resource
	private ClassDao classDao;

	@PostMapping("/classAdd")
	String add(ClassInfo c, ModelMap m) {
		classDao.save(c);
		m.put("msg", "班级信息添加成功");
		return "/admin/base_info/class_add.jsp";
	}

	// 查询所有班级信息
	@GetMapping("/classManager")
	String search(ModelMap m) {
		List<ClassInfo> classList = classDao.findAll(Sort.by(Sort.Direction.DESC, "id"));
		m.put("classList", classList);
		return "/admin/base_info/class_manager.jsp";
	}

	// 根据id查询出班级息转到修改页
	@GetMapping("/classUpdate/{id}")
	
	public String searchForUpdate(@PathVariable("id") Integer id, ModelMap m) {
		Optional<ClassInfo> classInfo = classDao.findById(id);
		m.put("classInfo", classInfo.get());
		return "/admin/base_info/class_update.jsp";
	}

	// 真正的进行修改
	@PostMapping("/classUpdate")
	public String update(ClassInfo classInfo, ModelMap m) {
		classDao.save(classInfo);
		m.put("c", classInfo);
		m.put("msg", "班级息修改成功!");
		return "/admin/base_info/class_update.jsp";
	}

	@GetMapping("/classDelete/{id}")
	public String delete(@PathVariable("id") Integer id, ModelMap m) throws ParseException {
		classDao.deleteById(id);
		m.put("msg", "班级信息删除成功");
		return search(m);
	}
}
